﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.ERPFramework.Core;
using System.ServiceModel;
using Kugar.ERPFramework.Core.Entity;
using System.Xml;
using System.Xml.Linq;
using Kugar.Core.ExtMethod;
using Kugar.ERPFramework.Core;
using Kugar.ERPFramework.BLLInterface;
using Kugar.ERPFramework.Core.Interfaces;

namespace Kugar.ERPFramework.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class SolutionService:ISolution
    {
        private Dictionary<string, Lazy<IProjectInstance>> _cacheInstance = new Dictionary<string, Lazy<IProjectInstance>>();
        private Dictionary<string, IProject> _cacheProject = new Dictionary<string, IProject>();
        private ProjectInfo[] _projectInfo = null;

        public SolutionService()
        {
            RefreshProjectInfo();
        }

        #region Implementation of ISolution

        [OperationContract]
        public ProjectInfo[] GetProjectList()
        {
            return _projectInfo;
        }

        [OperationContract]
        public string Connect()
        {
            return null;
        }


        #endregion


        public void RefreshProjectInfo()
        {
            _projectInfo = getProjectInfo();
        }

        private ProjectInfo[] getProjectInfo()
        {
            const string path = "Solution.xml";

            var document = XDocument.Load(path);

            var projects = document.Descendants("Project");

            var projectList = new List<ProjectInfo>(projects.Count());

            foreach (var projectXML in projects)
            {
                var tempProject = new ProjectInfo();

                tempProject.ProjectName = projectXML.Attribute("ProjectName").Value;
                tempProject.ProjectID = projectXML.Attribute("ProjectID").Value;
                tempProject.ProjectPath = projectXML.Attribute("Path").Value;

                _cacheProject.Add(tempProject.ProjectID,new Project(projectXML));

                //var instances = projectXML.Descendants("Instance");

                //var instanceList = new List<ProjectInstanceInfo>();

                //foreach (var instanceXML in instances)
                //{
                //    var instance = new ProjectInstanceInfo();
                //    instance.CreateDate = instanceXML.Attribute("CreateDate").Value;
                //    instance.InstanceName = instanceXML.Attribute("InstanceName").Value;
                //    instance.InstanceID = instanceXML.Attribute("InstanceID").Value;
                //    instance.ProjectID = tempProject.ProjectID;

                //    //_cacheInstance.Add(instance.InstanceID,new Lazy<IProjectInstance>(() =>
                //    //    {
                //    //        return new ProjectInstance(instance.InstanceName,tempProject,instance.InstanceID);
                //    //    }));

                //    instanceList.Add(instance);
                //}

                //tempProject.InstanceList = instanceList.ToArray();

                projectList.Add(tempProject);
            }

            return projectList.ToArray();
        }
    }
}
